package com.istudy.servlet;

import com.istudy.entity.Message;
import com.istudy.entity.Upload;
import com.istudy.entity.User;
import com.istudy.entity.Video;
import com.istudy.service.imp.UploadServiceImpl;
import com.istudy.utils.ReturnResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 视频上传servlet类
 * @date 2020-09-01
 * @author GQ
 */
@WebServlet("/uploadServlet")
public class UploadServlet extends AbstractServlet {
    UploadServiceImpl uploadService = new UploadServiceImpl();
    Message message= new Message();

    /**
     * 获取我们自己写的servlet类对象
     *
     * @return 1
     */
    @Override
    public Class<?> getServletClass() {
        return this.getClass();
    }

    public void insertUpload(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Upload upload = new Upload();
        String name= request.getParameter("name");
        String fileName= request.getParameter("fileName");
        String descrtion= request.getParameter("descrtion");
        User user =(User) request.getSession().getAttribute("user");
        String path="E:\\javaCode\\AIStudy\\web\\static\\audit";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        upload.setVideoUploadTime(df.format(new Date()));
        upload.setVideoUploadUser(user.getUserName());
        upload.setUserId(user.getUserId());
        upload.setVideoDescrtion(descrtion);
        upload.setVideoAddress(path);
        upload.setVideoName(name);
        boolean i = uploadService.addUpload(upload);
        PrintWriter out=response.getWriter();
        if(i){

            out.print("<script type='text/javascript'>alert('上传成功，1秒后将跳转至初始界面');"+
                    "setTimeout( window.location.href=document.referrer,1000);</script>");
        } else  {
            out.print("<script type='text/javascript'>alert('上传失败，请核对后再操作');"+
                    "location.reload();</script>");
        }

    }

    public String getList(HttpServletRequest request, HttpServletResponse response){
        int num=1;
        if(request.getParameter("currentPage")!=null) {
            num = Integer.parseInt(request.getParameter("currentPage"));
        }
       List<Upload> uploadList=uploadService.getAllList(request,num);
       request.setAttribute("uploadList",uploadList);
        return "auditlist";
    }



    public String going(HttpServletRequest request, HttpServletResponse response){
        if(request.getSession().getAttribute("user")!=null){
        return "upload";
        }else {
            return  "login";
        }
    }
    public String getUplaod(HttpServletRequest request, HttpServletResponse response){
        int num = Integer.parseInt(request.getParameter("id"));
        Upload upload=uploadService.getUpload(request,num);
        request.setAttribute("upload",upload);
        return "audit";
    }

    public Message deleteUpload(HttpServletRequest request, HttpServletResponse response){
        String id=request.getParameter("id");
        boolean i = uploadService.deleteUploadById(Integer.parseInt(id));
        if(i){
            message.setCode(1);
            message.setMessage("删除成功");
        } else  {
            message.setCode(-1);
            message.setMessage("删除失败");
        }

        return message;
    }

}
